local baseAttrInitValue = require("excel.Role").baseAttrInitValue[1]
local RoleDefine		= require("role.RoleDefine")
local Obj = require("core.Obj")
local max	 			= math.max
local random 			= math.random
local xianshi_xz		= baseAttrInitValue.xianshi_xz		--显示修正
local level_xz			= baseAttrInitValue.level_xz		--等级修正
local baoji_hurt_rate	= baseAttrInitValue.baoji_hurt_rate
local hurt_rate = baseAttrInitValue.hurt_rate
local ShenbingLogic = require("shenbing.ShenbingLogic")


-- 攻击方必须属性
local LEVEL							= RoleDefine.LEVEL
local HP_MAX						= RoleDefine.HP_MAX
local MP_MAX						= RoleDefine.MP_MAX
local HIT							= RoleDefine.HIT
local HUJIA_CT						= RoleDefine.HUJIA_CT
local ATK_YAZHI						= RoleDefine.ATK_YAZHI
local ATK							= RoleDefine.ATK
local BAOJI							= RoleDefine.BAOJI
local BAOJI_UP						= RoleDefine.BAOJI_UP
local ATTR_HIT_RATE					= RoleDefine.ATTR_HIT_RATE		--命中几率
local ATTR_BAO_JI_RATE				= RoleDefine.ATTR_BAO_JI_RATE	--暴击几率
local MAKE_SHANGHAI_RATE			= RoleDefine.MAKE_SHANGHAI_RATE	--造成伤害
local GIDDY                   = RoleDefine.GIDDY
local LUOMA                   = RoleDefine.LUOMA
local HUIXIN_RATE         = RoleDefine.HUIXIN_RATE      --会心一击
local ZHUOYUE_RATE        = RoleDefine.ZHUOYUE_RATE     --卓越一击
local MULTI_BAOJI_RATE    = RoleDefine.MULTI_BAOJI_RATE --双倍暴击
local FENG_ATK           = RoleDefine.FENG_ATK        --风属性伤害
local FIRE_ATK           = RoleDefine.FIRE_ATK        --火属性伤害
local WATER_ATK          = RoleDefine.WATER_ATK       --水属性伤害
local LEI_ATK            = RoleDefine.LEI_ATK         --雷属性伤害
local SKILL_HURT_UP      = RoleDefine.SKILL_HURT_UP   --技能伤害提升%


-- 防守方必须属性
local DEF_YAZHI						= RoleDefine.DEF_YAZHI
local SHANBI						= RoleDefine.SHANBI
local DEF							= RoleDefine.DEF
local DIKANG_CT						= RoleDefine.DIKANG_CT
local RENXING						= RoleDefine.RENXING
local BAOJI_DOWN					= RoleDefine.BAOJI_DOWN
local GEDANG_RATE					= RoleDefine.GEDANG_RATE
local GEDANG						= RoleDefine.GEDANG
local MONSTER_KX					= RoleDefine.MONSTER_KX
local MENGJIANG_KX						= RoleDefine.MENGJIANG_KX
local SHENMOU_KX					= RoleDefine.SHENMOU_KX
local XUNYING_KX					= RoleDefine.XUNYING_KX
local ATTR_DODGE_RATE				= RoleDefine.ATTR_DODGE_RATE 			--闪避几率
local ATTR_DEF_REN_XING_RATE		= RoleDefine.ATTR_DEF_REN_XING_RATE		--暴击抵抗几率
local RECV_SHANGHAI_RATE			= RoleDefine.RECV_SHANGHAI_RATE			--受伤降低
local GIDDY_KX                = RoleDefine.GIDDY_KX
local QISHU                   = RoleDefine.QISHU
local FENG_KX             = RoleDefine.FENG_KX     --风属性抗性
local FIRE_KX             = RoleDefine.FIRE_KX     --火属性抗性
local WATER_KX            = RoleDefine.WATER_KX     --水属性抗性
local LEI_KX              = RoleDefine.LEI_KX     --雷属性抗性

-- 预计算战斗属性						
local SHANG_HAI_JIA_SHEN			= RoleDefine.SHANG_HAI_JIA_SHEN			
local HIT_RATE						= RoleDefine.HIT_RATE
local BAO_JI_RATE					= RoleDefine.BAO_JI_RATE
local BAO_JI_RATE_ADDITION			= RoleDefine.BAO_JI_RATE_ADDITION
local SHANG_HAI_JIAN_MIAN			= RoleDefine.SHANG_HAI_JIAN_MIAN
local DODGE_RATE					= RoleDefine.DODGE_RATE
local DEF_REN_XING_RATE				= RoleDefine.DEF_REN_XING_RATE
local MONSTER_KANG_XING_JIAN_SHANG	= RoleDefine.MONSTER_KANG_XING_JIAN_SHANG
local MENGJIANG_KANG_XING_JIAN_SHANG = RoleDefine.MENGJIANG_KANG_XING_JIAN_SHANG
local SHENMOU_KANG_XING_JIAN_SHANG = RoleDefine.SHENMOU_KANG_XING_JIAN_SHANG
local XUNYING_KANG_XING_JIAN_SHANG = RoleDefine.XUNYING_KANG_XING_JIAN_SHANG
local GIDDY_RATE              = RoleDefine.GIDDY_RATE
local GIDDY_KX_RATE           = RoleDefine.GIDDY_KX_RATE
local LUOMA_RATE              = RoleDefine.LUOMA_RATE
local QISHU_RATE              = RoleDefine.QISHU_RATE

if not typeStrs then
	typeStrs	 = {}
	typeRateStrs = {}
	for i=0,1000 do
		typeStrs[i]		 = "makeShanghaiMonster" .. i
		typeRateStrs[i]  = "makeShanghaiRateMonster" .. i
	end
end

--① 命中计算

--攻击方命中几率	
function getHitRate(hit,lv,hitChance)
  lv = 150
	return 1 /(0.96 /(hit / ( lv * 250)*100)+0.01)/ 100 + hitChance
end

-- 防守方闪避几率
function getDodgeRate(shanbi,lv,shanbiRate)
  lv = 150
	return 1 /(0.96 /(shanbi / ( lv * 250)*100)+0.01)/ 100 + shanbiRate
end



--	②伤害计算

-- 防御穿透
function getDefCt(def,hujia_ct,dikang_ct,defCtRate,defCt)
	return max(def * (1 - max(hujia_ct/100 - dikang_ct/100,0))*(1-max(defCtRate,0)) - defCt,0) 
end

--抗性减伤率
function getKangXingJianShang(kangXing,xianshi_xz,sendLv,level_xz)
	 local kangXingJianShang --抗性减伤率
	 if kangXing ~= 0 then
		local v = kangXing * xianshi_xz
		sendLv = 150
		kangXingJianShang = v / (v + sendLv * level_xz)
	 else
		kangXingJianShang =	0																				
	 end
	 return kangXingJianShang
end

--伤害加深率
function getShangHaiJiaShen(atk_yazhi,lv)
	local shangHaiJiaShen
	if atk_yazhi ~= 0 then
	  lv = 150
		shangHaiJiaShen = 1 / (0.96 /(atk_yazhi / ( lv * 200)*100)+0.01)/ 100	
	else
		shangHaiJiaShen = 0
	end
	return shangHaiJiaShen
end

--伤害减免率
function getShangHaiJianMian(def_yazhi,recvLv)
	 local shangHaiJianMian
	 if def_yazhi ~= 0 then
	  recvLv = 150
		shangHaiJianMian = 1 /(0.96 /(def_yazhi / ( recvLv * 200)*100)+0.01)/ 100
	 else
		shangHaiJianMian = 0
	 end
	 return shangHaiJianMian
end

--标准攻击伤害
function getShangHai(sendAtk,recvDef,skillHurt)
	local shangHai
	if sendAtk * 0.75 <recvDef then			 --攻击不破防
		shangHai = sendAtk *(1 - 0.75)*(1- (recvDef - sendAtk * 0.75)/recvDef)* skillHurt
	else								
		shangHai = (sendAtk - recvDef)* skillHurt --攻击破防
	end	
	return shangHai
end

--造成伤害增加、减少
function getMakeShanghai(shangHai,shangHaiJiaShen,shangHaiJianMian,kangXingJianShang,makeShanghaiRate,recvShanghaiRate,makeShanghai,recvShanghai)
	return shangHai * (1 + (shangHaiJiaShen - shangHaiJianMian)) * (1 - kangXingJianShang) * (1 + (math.max(makeShanghaiRate - recvShanghaiRate,0))) + (math.max(makeShanghai - recvShanghai,0))
end

--攻击方暴击几率
function getBaoJiRate(baoji,sendLv,critChance)
  if baoji == 0 then return critChance end
  sendLv= 150
	return 1 /(0.96 /(baoji / ( sendLv * 250)*100) + 0.01) / 100 + critChance
end

--防守方韧性几率
function getDefRenXingRate(renxing,recvLv,renxingRate)
  if renxing == 0 then return renxingRate end
  recvLv = 150
	return 1 /(0.96 /(renxing / ( recvLv * 250)*100) + 0.01)/ 100 + renxingRate
end

--暴击百分比加成
function getBaojiRateAddition(baoji_hurt_rate,baojiUp,baojiDown)
  return baoji_hurt_rate + (baojiUp - baojiDown)
end

--攻击方落马几率
function getLuomaRate(luoma, sendLv, luomaRate)
  if luoma == 0 then return luomaRate end
  sendLv = 150
  return 1 /(0.96 /(luoma / ( sendLv * 500)*100) + 0.01)/ 100 + luomaRate
end
--防守方抗落马几率
function getQishuRate(qishu, recvLv, qishuRate)
  if qishu == 0 then return qishuRate end
  recvLv = 150
  return 1 /(0.96 /(qishu / ( recvLv * 500)*100) + 0.01)/ 100 + qishuRate
end

--攻击方眩晕几率
function getGiddyRate(giddy, sendLv, xuanyunRate)
  if giddy == 0 then return xuanyunRate  end
  sendLv = 150
  return 1 /(0.96 /(giddy / ( sendLv * 250)*100) + 0.01)/ 100 + xuanyunRate
end
--防守方抗晕几率
function getGiddyKXRate(giddy_kx, recvLv, kangyunRate)
  if giddy_kx == 0 then return kangyunRate end
  recvLv = 150
  return 1 /(0.96 /(giddy_kx / ( recvLv * 250)*100) + 0.01)/ 100 + kangyunRate
end

--伤害计算 
function hurt(obj,target,skillHurt,addition,critChance,hitChance,mainAttributes,mainAttributesAddition,mustHit,noDef, isNormalSkill)		--mustHit 必定击中	
	local isHit			-- 是否命中
	local isHeavyHit	-- 是否暴击
	local isGedang		-- 是否格挡
	local isGiddy     -- 是否眩晕
	local isLuoma     -- 是否落马
	local objAttr		= obj.attr
	local objBufAttr	= obj.bufAttr
	local targetAttr	= target.attr
	local targetBufAttr = target.bufAttr
--	local recvLv		= targetAttr[LEVEL]
	local objType		= obj.obj_type
	local targetType	= target.obj_type
	
	if mainAttributes and mainAttributes ~=0 then
		addition = addition + objAttr[mainAttributes] * mainAttributesAddition
	end
	--技能伤害提升%
  if isNormalSkill == 0 and objAttr[SKILL_HURT_UP] then
    skillHurt = skillHurt+objAttr[SKILL_HURT_UP]
  end
	--	① 命中计算
	--attr[HIT_RATE]	--攻击方命中几率	
	--attr[DODGE_RATE]	-- 防守方闪避几率
	if not mustHit then
	  local  hitRate = (objAttr[HIT_RATE]+hitChance) - targetAttr[DODGE_RATE] + 0.95
	  hitRate = hitRate< 0.16 and 0.16 or hitRate
	  if random(1,100)*0.01 >= hitRate then
		  return isHit --攻击未命中，忽略之后所有计算
		end
	end		

	isHit = true
	
	--	②伤害计算	
	 
	 --防守方防御值		 
	local def		= targetAttr[DEF]
	if objType == Obj.TYPE_HUMAN and targetType == Obj.TYPE_HUMAN then
     def = def * 0.5
  end
	local hujia_ct	= objAttr[HUJIA_CT]
	local dikang_ct= targetAttr[DIKANG_CT]
	local defCtRate= objBufAttr.defCtRate
	local defCt	= objBufAttr.defCt
	local difCt	= (hujia_ct - dikang_ct)--/100

	if difCt<0 then difCt = 0 end
	if defCtRate<0 then defCtRate = 0 end

	local recvDef
	if noDef then
		recvDef = 0
	else
		recvDef = def * (1 - difCt ) * (1-defCtRate) - defCt -- 防御穿透
		if recvDef<0 then recvDef = 0 end
	end

	--抗性
	local kangXing
	-- 100 -> 10-20
	if objType==Obj.TYPE_HUMAN then	 
	    local sbType = ShenbingLogic.getShenbingType(obj)
      if sbType == 1 then
			   kangXing = targetAttr[MENGJIANG_KX]
			elseif sbType == 2 then
			   kangXing = targetAttr[SHENMOU_KX]
			elseif sbType == 3 then
			   kangXing = targetAttr[XUNYING_KX]
			else
			   kangXing = 0
			end
	else
		  kangXing = targetAttr[MONSTER_KX]
	end

	--抗性减伤率
	local kangXingJianShang = 0
	if kangXing ~= 0 then
	   local v = kangXing * xianshi_xz
		 kangXingJianShang = v / (v + 150 * level_xz)
	end

	--攻击方攻击力
	local sendAtk = objAttr[ATK] + addition
	local sendAtkMax =  sendAtk * 0.75							
	--标准攻击伤害
	local shangHai 
	if sendAtkMax <recvDef then			 --攻击不破防
		local sendAtkMin =  sendAtk - sendAtkMax
		shangHai = sendAtkMin * (1-(recvDef - sendAtkMax)/recvDef) * skillHurt
	else								
		shangHai = (sendAtk - recvDef) * skillHurt --攻击破防
	end	

	local makeShanghai		= objBufAttr.makeShanghai
	local makeShanghaiRate	= objBufAttr.makeShanghaiRate
	local recvShanghai		= targetBufAttr.recvShanghai
	local recvShanghaiRate	= targetBufAttr.recvShanghaiRate
	 
	if targetType == Obj.TYPE_MONSTER and objBufAttr.makeShanghaiMonster   then
		local monsterType					= target.monsterType
		local makeShanghaiMonsterType		= typeStrs[monsterType]
		local makeShanghaiRateMonsterType	= typeRateStrs[monsterType]
		local makeShanghaiMonster			= objBufAttr[makeShanghaiMonsterType]
		local makeShanghaiRateMonster		= objBufAttr[makeShanghaiRateMonsterType]

		if makeShanghaiMonster then
			makeShanghai = makeShanghai + makeShanghaiMonster
		end

		if makeShanghaiRateMonster then
			makeShanghaiRate = makeShanghaiRate + makeShanghaiRateMonster
		end
	end
	
	makeShanghaiRate = makeShanghaiRate + objAttr[MAKE_SHANGHAI_RATE]
	recvShanghaiRate = recvShanghaiRate * -1 +  targetAttr[RECV_SHANGHAI_RATE]
	recvShanghai	 = recvShanghai * -1

	 --造成伤害增加、减少
	local shangHaiJiaShen	= objAttr[SHANG_HAI_JIA_SHEN] + makeShanghaiRate
	local shangHaiJianMian = targetAttr[SHANG_HAI_JIAN_MIAN] + recvShanghaiRate
	shangHai =  shangHai * (1 + (shangHaiJiaShen - shangHaiJianMian)) * (1 - kangXingJianShang)  +(makeShanghai - recvShanghai)
--	③暴击计算
   local noBaoJiShanghai = shangHai
	 if random(1, 1000)*0.001 <= objAttr[MULTI_BAOJI_RATE] + critChance then --双倍暴击
	    if random(1, 1000)*0.001 > targetAttr[DEF_REN_XING_RATE] then
	       shangHai = shangHai * 2
	       isHeavyHit = 3
	    end
	 elseif random(1, 1000)*0.001 <= objAttr[ZHUOYUE_RATE] then-- - targetAttr[DEF_REN_XING_RATE] then --卓越一击
	    if random(1, 1000)*0.01 > targetAttr[DEF_REN_XING_RATE] then
	       shangHai = shangHai * 1.5
	       isHeavyHit = 2
	    end
	 elseif random(1, 1000)*0.001 <= objAttr[HUIXIN_RATE]  then -- - targetAttr[DEF_REN_XING_RATE] then  --会心一击
	    if random(1, 1000)*0.001 > targetAttr[DEF_REN_XING_RATE] then
	       shangHai = shangHai * 1.2
	       isHeavyHit = 1
	    end
	 end
	
-- ④格挡计算
	if not mustHit and random(1,100)*0.01 < targetAttr[GEDANG_RATE] then
		shangHai = shangHai - targetAttr[GEDANG]
		isGedang = true
	end
	
--⑥眩晕计算
  if random(1,100)*0.01 < objAttr[GIDDY_RATE] - targetAttr[GIDDY_KX_RATE] then
	  isGiddy = true
	end

--⑦落马计算
  if random(1,100)*0.01 < objAttr[LUOMA_RATE] - targetAttr[QISHU_RATE] then
    isLuoma = true
  end
  

--元素伤害计算
  local hv = objAttr[FENG_ATK] - targetAttr[FENG_KX] 
  if hv >0 then shangHai = shangHai + hv end
  hv = objAttr[FIRE_ATK] - targetAttr[FIRE_KX] 
  if hv >0 then shangHai = shangHai + hv end
  hv = objAttr[WATER_ATK] - targetAttr[WATER_KX] 
  if hv >0 then shangHai = shangHai + hv end  
  hv = objAttr[LEI_ATK] - targetAttr[LEI_KX] 
  if hv >0 then shangHai = shangHai + hv end
    
	shangHai = shangHai * random((1-hurt_rate)*100,(1+hurt_rate)*100)*0.01	
	
	if objType == Obj.TYPE_HUMAN or targetType == Obj.TYPE_HUMAN then
	   shangHai = shangHai * 0.6 
	   if objType == Obj.TYPE_HUMAN and targetType == Obj.TYPE_HUMAN then
	       shangHai = shangHai * 0.7 
	   end
	elseif targetType == Obj.TYPE_PET then
	   shangHai = shangHai * 0.2
	elseif objType == Obj.TYPE_AOE or objType == Obj.TYPE_TRAP then
	   local owner = Obj.getOwner(obj)
	   if owner and owner.obj_type == Obj.TYPE_HUMAN then
	       shangHai = shangHai*0.6
	       if targetType == Obj.TYPE_HUMAN then
	           shangHai = shangHai * 0.7 
	       end
	   end
  end
  if objType == Obj.TYPE_PET and objType == Obj.TYPE_HUMAN then--根据攻击和被攻击的对象类型来计算百分比
    shangHai = shangHai * 0.5
  elseif objType == Obj.TYPE_MONSTER and objType == Obj.TYPE_PET then
    shangHai = shangHai * 0.5
  elseif objType == Obj.TYPE_HUMAN and objType == Obj.TYPE_PET then
    shangHai = shangHai * 0.7
  end
  
  if target.maxHurt and target.maxHurt > 0 and target.maxHurt < shangHai then
     shangHai = target.maxHurt
	end
  if shangHai < 5 then
      shangHai = random(1,5)
  end 
	
	return isHit,isHeavyHit,isGedang,shangHai,noBaoJiShanghai,isGiddy,isLuoma
end


-- 战斗系统需要用的属性(Obj 创建时调用)
function initAttr(obj)
	local attr  = obj.attr
	if not attr then 
		attr = {}
		obj.attr = attr
	end
	
	local bufAttr  = obj.bufAttr
	if not bufAttr then 
		bufAttr = {}
		obj.bufAttr = bufAttr
	end
	
	attr[LEVEL]		 		= 1
	
	-- 攻击方必须属性
	attr[HIT]		 		= 0
	attr[HUJIA_CT]			= 0
	attr[ATK_YAZHI]			= 0
	attr[ATK]		 		= 0
	attr[BAOJI]		 		= 0
	attr[BAOJI_UP]			= 0
	attr[ATTR_HIT_RATE]		= 0
	attr[ATTR_BAO_JI_RATE]	= 0
	attr[MAKE_SHANGHAI_RATE]= 0
	attr[GIDDY] = 0
	attr[LUOMA] = 0
	
	bufAttr.baojiRate		= 0
	bufAttr.makeShanghai	= 0
	bufAttr.makeShanghaiRate= 0
	bufAttr.hitRate			= 0
  attr[HUIXIN_RATE]         = 0     --会心一击
  attr[ZHUOYUE_RATE]        = 0     --卓越一击
  attr[MULTI_BAOJI_RATE]    = 0     --双倍暴击
  attr[FENG_ATK]           = 0     --风属性伤害
  attr[FIRE_ATK]           = 0     --火属性伤害
  attr[WATER_ATK]          = 0     --水属性伤害
  attr[LEI_ATK]            = 0     --雷属性伤害
  
	-- 防守方必须属性
	attr[DEF_YAZHI]				= 0
	attr[SHANBI]				= 0
	attr[DEF]		 			= 0
	attr[DIKANG_CT]				= 0
	attr[RENXING]				= 0
	attr[BAOJI_DOWN]			= 0
	attr[GEDANG_RATE]			= 0
	attr[GEDANG]				= 0
	attr[MONSTER_KX]			= 0
	attr[MENGJIANG_KX] =0
	attr[SHENMOU_KX] = 0
	attr[XUNYING_KX] = 0
	attr[ATTR_DODGE_RATE]		= 0
	attr[ATTR_DEF_REN_XING_RATE]= 0
	attr[RECV_SHANGHAI_RATE]	= 0
	attr[GIDDY_KX] = 0
	attr[QISHU] = 0
	
  attr[FENG_KX]             = 0     --风属性抗性
  attr[FIRE_KX]             = 0     --火属性抗性
  attr[WATER_KX]            = 0     --水属性抗性
  attr[LEI_KX]              = 0     --雷属性抗性
  
	bufAttr.shanbiRate		= 0
	bufAttr.recvShanghai	= 0
	bufAttr.recvShanghaiRate= 0
	bufAttr.renxingRate		= 0
	bufAttr.defCtRate		= 0 
	bufAttr.defCt			= 0
	
	
	-- 预计算战斗属性
	attr[SHANG_HAI_JIA_SHEN]			= 0
	attr[HIT_RATE]						= 0
	attr[BAO_JI_RATE]					= 0
	attr[BAO_JI_RATE_ADDITION]			= 0
	attr[SHANG_HAI_JIAN_MIAN]			= 0 
	attr[DODGE_RATE]					= 0
	attr[DEF_REN_XING_RATE]				= 0
	attr[GIDDY_RATE] = 0
	attr[GIDDY_KX_RATE] = 0
	attr[LUOMA_RATE] = 0
	attr[QISHU_RATE] = 0

	if obj.db then
		attr[MONSTER_KANG_XING_JIAN_SHANG]	= 0
		attr[MENGJIANG_KANG_XING_JIAN_SHANG]	= 0
		attr[SHENMOU_KANG_XING_JIAN_SHANG]	= 0
		attr[XUNYING_KANG_XING_JIAN_SHANG]	= 0
	end
end

function calcAttr(obj)
	local attr		= obj.attr
	local bufAttr	= obj.bufAttr
	local level		= attr[LEVEL]
	
	attr[SHANG_HAI_JIA_SHEN]				= getShangHaiJiaShen	(attr[ATK_YAZHI], level)
	attr[HIT_RATE]							= getHitRate			(attr[HIT], level, attr[ATTR_HIT_RATE] +  bufAttr.hitRate)
	attr[BAO_JI_RATE]						= getBaoJiRate			(attr[BAOJI], level, attr[ATTR_BAO_JI_RATE] +  bufAttr.baojiRate)	
	attr[BAO_JI_RATE_ADDITION]				= getBaojiRateAddition	(baoji_hurt_rate, attr[BAOJI_UP], 0)
	attr[SHANG_HAI_JIAN_MIAN]				= getShangHaiJianMian	(attr[DEF_YAZHI], level)	
	attr[DODGE_RATE]						= getDodgeRate			(attr[SHANBI], level, attr[ATTR_DODGE_RATE] + bufAttr.shanbiRate)		
	attr[DEF_REN_XING_RATE]					= getDefRenXingRate		(attr[RENXING], level, attr[ATTR_DEF_REN_XING_RATE] + bufAttr.renxingRate)
	attr[GIDDY_RATE] = getGiddyRate(attr[GIDDY], level, bufAttr.xuanyunRate or 0)
  attr[GIDDY_KX_RATE] = getGiddyKXRate(attr[GIDDY_KX], level,bufAttr.kangyunRate or 0)
  attr[LUOMA_RATE] = getLuomaRate(attr[LUOMA], level, bufAttr.luomaRate or 0)
  attr[QISHU_RATE] = getQishuRate(attr[QISHU], level, bufAttr.qishuRate or 0)
	if obj.db then
		attr[MONSTER_KANG_XING_JIAN_SHANG]	= getKangXingJianShang(attr[MONSTER_KX], xianshi_xz, level, level_xz) 
		attr[MENGJIANG_KANG_XING_JIAN_SHANG] = getKangXingJianShang(attr[MENGJIANG_KX], xianshi_xz, level, level_xz) 
		attr[SHENMOU_KANG_XING_JIAN_SHANG] = getKangXingJianShang(attr[SHENMOU_KX], xianshi_xz, level, level_xz) 		
		attr[XUNYING_KANG_XING_JIAN_SHANG] = getKangXingJianShang(attr[XUNYING_KX], xianshi_xz, level, level_xz) 
	end
end

function setAttrMount(obj, target)
	local attr  = obj.attr
	if not attr then 
		attr = {}
		obj.attr = attr
	end
	
	local bufAttr  = obj.bufAttr
	if not bufAttr then 
		bufAttr = {}
		obj.bufAttr = bufAttr
	end
	
	local targetAttr			= target.attr
	local targetBufAttr			= target.bufAttr
	
	attr[LEVEL]		 			= targetAttr[LEVEL]
	attr[HP_MAX]		 		= targetAttr[HP_MAX]
	attr[MP_MAX]		 		= targetAttr[MP_MAX]

	
	-- 攻击方必须属性
	attr[HIT]		 			= targetAttr[HIT]
	attr[HUJIA_CT]				= targetAttr[HUJIA_CT]
	attr[ATK_YAZHI]				= targetAttr[ATK_YAZHI]
	attr[ATK]		 			= targetAttr[ATK]
	attr[BAOJI]		 			= targetAttr[BAOJI]
	attr[BAOJI_UP]				= targetAttr[BAOJI_UP]
	attr[ATTR_HIT_RATE]			= targetAttr[ATTR_HIT_RATE]
	attr[ATTR_BAO_JI_RATE]		= targetAttr[ATTR_BAO_JI_RATE]
	attr[MAKE_SHANGHAI_RATE]	= targetAttr[MAKE_SHANGHAI_RATE]
	attr[GIDDY] = targetAttr[GIDDY]
  attr[LUOMA] = targetAttr[LUOMA]

  
	bufAttr.baojiRate			= targetBufAttr.baojiRate
	bufAttr.makeShanghai		= targetBufAttr.makeShanghai
	bufAttr.makeShanghaiRate	= targetBufAttr.makeShanghaiRate
	bufAttr.hitRate				= targetBufAttr.hitRate
  attr[HUIXIN_RATE]         = targetAttr[HUIXIN_RATE]     --会心一击
  attr[ZHUOYUE_RATE]        = targetAttr[ZHUOYUE_RATE]     --卓越一击
  attr[MULTI_BAOJI_RATE]    = targetAttr[MULTI_BAOJI_RATE]     --双倍暴击
  attr[FENG_ATK]           = targetAttr[FENG_ATK]     --风属性伤害
  attr[FIRE_ATK]           = targetAttr[FIRE_ATK]     --火属性伤害
  attr[WATER_ATK]          = targetAttr[WATER_ATK]    --水属性伤害
  attr[LEI_ATK]            = targetAttr[LEI_ATK]      --雷属性伤害

	-- 防守方必须属性
	attr[DEF_YAZHI]				= targetAttr[DEF_YAZHI]		
	attr[SHANBI]				= targetAttr[SHANBI]		
	attr[DEF]		 			= targetAttr[DEF]		 	
	attr[DIKANG_CT]				= targetAttr[DIKANG_CT]		
	attr[RENXING]				= targetAttr[RENXING]		
	attr[BAOJI_DOWN]			= targetAttr[BAOJI_DOWN]	
	attr[GEDANG_RATE]			= targetAttr[GEDANG_RATE]	
	attr[GEDANG]				= targetAttr[GEDANG]		
	attr[MONSTER_KX]			= targetAttr[MONSTER_KX]	
	attr[MENGJIANG_KX] =  targetAttr[MENGJIANG_KX]
  attr[SHENMOU_KX] = targetAttr[SHENMOU_KX]
  attr[XUNYING_KX] = targetAttr[XUNYING_KX]

	attr[ATTR_DODGE_RATE]		= targetAttr[ATTR_DODGE_RATE]	
	attr[ATTR_DEF_REN_XING_RATE]= targetAttr[ATTR_DEF_REN_XING_RATE]
	attr[RECV_SHANGHAI_RATE]	= targetAttr[RECV_SHANGHAI_RATE]
	attr[GIDDY_KX] = targetAttr[GIDDY_KX]
  attr[QISHU] = targetAttr[QISHU]
	
  attr[FENG_KX]             = targetAttr[FENG_KX]     --风属性抗性
  attr[FIRE_KX]             = targetAttr[FIRE_KX]     --火属性抗性
  attr[WATER_KX]            = targetAttr[WATER_KX]     --水属性抗性
  attr[LEI_KX]              = targetAttr[LEI_KX]     --雷属性抗性
  
  attr[SKILL_HURT_UP]       = targetAttr[SKILL_HURT_UP] 
  
	bufAttr.shanbiRate			= targetBufAttr.shanbiRate		
	bufAttr.recvShanghai		= targetBufAttr.recvShanghai	
	bufAttr.recvShanghaiRate	= targetBufAttr.recvShanghaiRate
	bufAttr.renxingRate			= targetBufAttr.renxingRate		
	bufAttr.defCtRate			= targetBufAttr.defCtRate		
	bufAttr.defCt				= targetBufAttr.defCt			
	
	
	-- 预计算战斗属性
	attr[SHANG_HAI_JIA_SHEN]	= targetAttr[SHANG_HAI_JIA_SHEN]	
	attr[HIT_RATE]				= targetAttr[HIT_RATE]				
	attr[BAO_JI_RATE]			= targetAttr[BAO_JI_RATE]			
	attr[BAO_JI_RATE_ADDITION]	= targetAttr[BAO_JI_RATE_ADDITION]	
	attr[SHANG_HAI_JIAN_MIAN]	= targetAttr[SHANG_HAI_JIAN_MIAN]	
	attr[DODGE_RATE]			= targetAttr[DODGE_RATE]			
	attr[DEF_REN_XING_RATE]		= targetAttr[DEF_REN_XING_RATE]		
	attr[GIDDY_RATE] = targetAttr[GIDDY_RATE]
  attr[GIDDY_KX_RATE] = targetAttr[GIDDY_KX_RATE]
  attr[LUOMA_RATE] = targetAttr[LUOMA_RATE]
  attr[QISHU_RATE] = targetAttr[QISHU_RATE]
	if target.db then
		attr[MONSTER_KANG_XING_JIAN_SHANG]	= targetAttr[MONSTER_KANG_XING_JIAN_SHANG]	
    attr[MENGJIANG_KANG_XING_JIAN_SHANG] =targetAttr[MENGJIANG_KANG_XING_JIAN_SHANG] 
    attr[SHENMOU_KANG_XING_JIAN_SHANG] =  targetAttr[SHENMOU_KANG_XING_JIAN_SHANG]   
    attr[XUNYING_KANG_XING_JIAN_SHANG] =  targetAttr[XUNYING_KANG_XING_JIAN_SHANG]
	end
end
